package com.minivision.sdktestdemo.ui.view;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.minivision.sdktestdemo.R;
import com.minivision.sdktestdemo.bean.MusicBean;
import com.minivision.sdktestdemo.event.PlayMusicEvent;
import com.minivision.sdktestdemo.theme.utils.ThemeUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by devtang on 2016/5/24.
 */
public class MusicListPopItemRecyclerAdapter extends RecyclerView.Adapter {
    private List<MusicBean> mBeans;
    private PlayMusicEvent mEvent;
    private OnItemClickListener mListener;

    public interface OnItemClickListener {
        void onClickItem(int position);

        void onClickRemove(int postion);

        void onClickLocate(int postion);
    }

    public MusicListPopItemRecyclerAdapter() {
        mBeans = new ArrayList<>();
        mEvent = null;
    }

    public List<MusicBean> getList() {
        return mBeans;
    }

    public PlayMusicEvent getEvent() {
        return mEvent;
    }

    public void setmBeans(List<MusicBean> mBeans) {
        this.mBeans = mBeans;
    }

    public void setmEvent(PlayMusicEvent mEvent) {
        this.mEvent = mEvent;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.pop_music_list_view_item, parent, false);
        return new MusicListPopItemHolder(view);
    }

    public int getEventPosition(PlayMusicEvent event) {
        int index = 0;
        for (MusicBean bean : mBeans) {
            if (bean.getmPath().equals(event.mUri)) {
                return index;
            }
            ++index;
        }
        return -1;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        MusicListPopItemHolder itemHolder = (MusicListPopItemHolder) holder;
        if (mEvent.mUri.equals(mBeans.get(position).getmPath())) {
            itemHolder.mIconLocate.setVisibility(View.VISIBLE);
            itemHolder.mIconSounds.setVisibility(View.VISIBLE);
            itemHolder.mTextContent.setSelected(true);
        } else {
            itemHolder.mIconLocate.setVisibility(View.GONE);
            itemHolder.mIconSounds.setVisibility(View.GONE);
            itemHolder.mTextContent.setSelected(false);
        }
        itemHolder.mTextContent.setText(mBeans.get(position).getmMusicName() +
                " - " + mBeans.get(position).getmSingerName());
        itemHolder.setOnItemClickListener(mListener, itemHolder.getLayoutPosition());
        ThemeUtils.initTheme(holder.itemView);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    @Override
    public int getItemCount() {
        return mBeans.size();
    }

    public class MusicListPopItemHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.icon_play)
        SimpleDraweeView mIconSounds;

        @Bind(R.id.icon_locate)
        SimpleDraweeView mIconLocate;

        @Bind(R.id.icon_remove)
        SimpleDraweeView mIconRemove;

        @Bind(R.id.tv_content)
        TextView mTextContent;

        public MusicListPopItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setOnItemClickListener(final OnItemClickListener listener, final int position) {
            if (listener != null) {
                if (mIconLocate.getVisibility() == View.VISIBLE) {
                    mIconLocate.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            listener.onClickLocate(position);
                        }
                    });
                }
                mIconRemove.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        listener.onClickRemove(position);
                    }
                });
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mListener.onClickItem(position);
                    }
                });
            }
        }
    }
}
